aboutsummaryrefslogtreecommitdiff
path: root/pages/anime/watch/[...info].js
blob: ed1a50b8bcb0086fbe8fa43a00e378813a6681a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import Layout from "../../../components/layout";
// import { data } from "../../../lib/testData";
// import { aniData } from "../../../lib/infoData";
import Image from "next/image";
import VideoPlayer from "../../../components/videoPlayer";
import Link from "next/link";
import { closestMatch } from "closest-match";
import Head from "next/head";
import { useEffect, useState } from "react";
import Modal from "../../../components/modal";

import { useNotification } from "../../../lib/useNotify";

import { useSession, signIn, signOut } from "next-auth/react";
import AniList from "../../../components/media/aniList";

import { AnimatePresence, motion as m } from "framer-motion";
import Navbar from "../../../components/navbar";
import { Navigasi } from "../..";

export default function Info({ info }) {
  const { data: session, status } = useSession();
  const title = info.aniData.title.romaji || info.aniData.title.english;
  const data = info.aniData;
  const fallback = info.epiFallback;
  const { Notification: NotificationComponent, show } = useNotification();

  // console.log(session);

  const playingEpisode = data.episodes
    .filter((item) => item.id == info.id)
    .map((item) => item.number);

  const [open, setOpen] = useState(false);
  const [aniStatus, setAniStatus] = useState("");
  const [aniProgress, setAniProgress] = useState(parseInt(playingEpisode));

  const handleStatus = (e) => {
    setAniStatus(e.target.value);
  };

  const handleProgress = (e) => {
    const value = parseFloat(e.target.value);
    if (!isNaN(value) && value >= 0 && value <= data.totalEpisodes) {
      setAniProgress(value);
    }
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    const formData = { status: aniStatus, progress: aniProgress };
    console.log(formData);
  };

  const playingTitle = data.episodes
    .filter((item) => item.id == info.id)
    .map((item) => item.title);

  if (status === "loading") {
    return <p>Loading...</p>;
  }

  console.log(parseInt(playingEpisode));

  return (
    <>
      <Head>
        <title>
          {fallback ? data.title.romaji || data.title.english : playingTitle}
        </title>
      </Head>

      <NotificationComponent />

      <Modal open={open} onClose={() => setOpen(false)}>
        <div className="bg-[#202020] rounded-lg text-center">
          <div className="p-5 grid gap-2 justify-center place-items-center">
            <h1 className="text-md font-extrabold font-karla">
              Save this Anime to Your List
            </h1>
            {!session && (
              <button
                className="flex items-center bg-[#3a3a3a] mt-4 rounded-md text-white p-1"
                onClick={() => signIn("AniListProvider")}
              >
                <h1 className="px-1 font-bold font-karla">
                  Login with AniList
                </h1>
                <div className="scale-[60%] pb-[1px]">
                  <AniList />
                </div>
              </button>
            )}
            {session && (
              <>
                <form
                  onSubmit={handleSubmit}
                  className="grid grid-cols-2 gap-5 max-w-sm mx-auto mt-5 items-center"
                >
                  <div className="mb-4">
                    <label
                      htmlFor="option"
                      className="block font-bold mb-2 text-sm"
                    >
                      Select an option
                    </label>
                    <select
                      id="option"
                      value={aniStatus}
                      onChange={handleStatus}
                      className="form-select block w-full px-2 py-1 rounded-lg shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300"
                    >
                      {aniStatus === "" && (
                        <option value="" hidden>
                          Select an option
                        </option>
                      )}
                      <option value="option1">Option 1</option>
                      <option value="option2">Option 2</option>
                      <option value="option3">Option 3</option>
                    </select>
                  </div>
                  <div className="mb-4">
                    <label
                      htmlFor="number"
                      className="block text-sm font-bold mb-2"
                    >
                      Episode Progress
                    </label>
                    <input
                      id="number"
                      type="number"
                      step="1"
                      min="0"
                      max={data.totalEpisodes}
                      className="form-input block w-full px-2 py-1 rounded-lg shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300"
                      value={aniProgress}
                      onChange={handleProgress}
                    />
                  </div>
                  <div className="col-start-2 row-start-2 w-full justify-items-end text-center">
                    <button
                      type="submit"
                      className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
                      onClick={() => setOpen(false)}
                    >
                      Submit
                    </button>
                  </div>
                </form>
              </>
            )}
          </div>
        </div>
      </Modal>
      <Navigasi />
      <div className="min-h-screen flex flex-col lg:gap-0 gap-5 lg:flex-row lg:py-10 lg:px-10 justify-start w-screen">
        <div className="w-screen lg:w-[67%]">
          <div className="h-auto aspect-video z-20">
            <VideoPlayer
              key={info.id}
              data={info.epiData}
              seek={info.seek}
              titles={title}
              id={info.id}
              progress={parseInt(playingEpisode)}
              session={session}
              aniId={parseInt(data.id)}
            />
          </div>
          <div>
            <div className="">
              {data.episodes.length > 0 ? (
                data.episodes
                  .filter((items) => items.id == info.id)
                  .map((item) => (
                    <div key={item.id} className="p-3 grid gap-2">
                      <div className="text-xl font-outfit font-semibold line-clamp-2">
                        <Link
                          href={`/anime/${data.id}`}
                          className="inline hover:underline"
                        >
                          {item.title}
                        </Link>
                      </div>
                      <h4 className="text-sm font-karla font-light">
                        Episode {item.number}
                      </h4>
                    </div>
                  ))
              ) : (
                <>
                  {fallback
                    .filter((item) => item.id == info.id)
                    .map((item) => (
                      <div key={item.id} className="p-3 grid gap-2">
                        <div className="text-xl font-outfit font-semibold line-clamp-2">
                          <Link
                            href={`/anime/${data.id}`}
                            className="inline hover:underline"
                          >
                            {title}
                          </Link>
                        </div>
                        <h4 className="text-sm font-karla font-light">
                          Episode {item.number}
                        </h4>
                      </div>
                    ))}
                </>
              )}
            </div>
            <div className="h-[1px] bg-[#3b3b3b]" />
            <div>
              <div className="px-4 pt-7 pb-4 h-full flex">
                <div className="aspect-[9/13] h-[240px]">
                  <Image
                    src={data.image}
                    alt="Anime Cover"
                    width={1000}
                    height={1000}
                    className="object-cover aspect-[9/13] h-[240px] rounded-md"
                  />
                </div>
                <div className="grid w-full px-5 gap-3 h-[240px]">
                  <div className="grid grid-cols-2 gap-1 items-center">
                    <h2 className="text-sm font-light font-roboto text-[#878787]">
                      Studios
                    </h2>
                    <div className="row-start-2">{data.studios}</div>
                    <div className="grid col-start-2 place-content-end relative">
                      <div className="" onClick={() => setOpen(true)}>
                        <svg
                          xmlns="http://www.w3.org/2000/svg"
                          fill="none"
                          viewBox="0 0 24 24"
                          strokeWidth={1.5}
                          stroke="currentColor"
                          className="w-8 h-8 hover:fill-white hover:cursor-pointer"
                        >
                          <path
                            strokeLinecap="round"
                            strokeLinejoin="round"
                            d="M17.593 3.322c1.1.128 1.907 1.077 1.907 2.185V21L12 17.25 4.5 21V5.507c0-1.108.806-2.057 1.907-2.185a48.507 48.507 0 0111.186 0z"
                          />
                        </svg>
                        {/* <span className=" transition-all duration-300 absolute -top-12 bg-[#2c2c2c] font-karla p-2 rounded-l-lg rounded-tr-lg right-3 select-none">
                          Save to My List
                        </span> */}
                      </div>
                    </div>
                  </div>
                  <div className="grid gap-1 items-center">
                    <h2 className="text-sm font-light font-roboto text-[#878787]">
                      Status
                    </h2>
                    <div>{data.status}</div>
                  </div>
                  <div className="grid gap-1 items-center overflow-y-hidden">
                    <h2 className="text-sm font-light font-roboto text-[#878787]">
                      Titles
                    </h2>
                    <div className="grid grid-flow-dense grid-cols-2 gap-2 h-full w-full">
                      <div className="line-clamp-3">
                        {data.title.romaji || ""}
                      </div>
                      <div className="line-clamp-3">
                        {data.title.english || ""}
                      </div>
                      <div className="line-clamp-3">
                        {data.title.native || ""}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div className="flex flex-wrap gap-3 px-4 pt-3 ">
                {data.genres.map((item, index) => (
                  <div
                    key={index}
                    className="border border-action text-gray-100 py-1 px-2 rounded-md font-karla text-sm"
                  >
                    {item}
                  </div>
                ))}
              </div>
              <div className={`bg-[#2a2a2a] rounded-md mt-3 mx-3`}>
                <p
                  dangerouslySetInnerHTML={{ __html: data.description }}
                  className={`p-5 text-sm font-light font-roboto text-[#e4e4e4] `}
                />
              </div>
            </div>
          </div>
        </div>
        <div className="flex flex-col w-screen lg:w-[33%] ">
          <h1 className="text-xl font-karla pl-4 pb-5 font-semibold">
            Episodes
          </h1>
          <div className="grid gap-5 lg:px-5 px-2 py-2 scrollbar-thin scrollbar-thumb-[#313131] scrollbar-thumb-rounded-full">
            {data.episodes.length > 0
              ? data.episodes.map((item) => {
                  return (
                    <Link
                      href={`/anime/watch/${item.id}/${data.id}`}
                      key={item.id}
                      className={`bg-secondary flex w-full h-[110px] rounded-lg scale-100 transition-all duration-300 ease-out ${
                        item.id == info.id
                          ? "pointer-events-none ring-1 ring-action"
                          : "cursor-pointer hover:scale-[1.02] ring-0 hover:ring-1 hover:shadow-lg ring-white"
                      }`}
                    >
                      <div className="w-[40%] h-full relative shrink-0">
                        <Image
                          src={item.image}
                          alt="image"
                          height={1000}
                          width={1000}
                          className={`object-cover rounded-lg h-[110px] shadow-[4px_0px_5px_0px_rgba(0,0,0,0.3)] ${
                            item.id == info.id
                              ? "brightness-[30%]"
                              : "brightness-75"
                          }`}
                        />
                        <span className="absolute bottom-2 left-2 font-karla font-light text-sm">
                          Episode {item.number}
                        </span>
                        {item.id == info.id && (
                          <div className="absolute top-11 left-[105px] scale-[1.5]">
                            <svg
                              xmlns="http://www.w3.org/2000/svg"
                              viewBox="0 0 20 20"
                              fill="currentColor"
                              className="w-5 h-5"
                            >
                              <path d="M6.3 2.841A1.5 1.5 0 004 4.11V15.89a1.5 1.5 0 002.3 1.269l9.344-5.89a1.5 1.5 0 000-2.538L6.3 2.84z" />
                            </svg>
                          </div>
                        )}
                      </div>
                      <div
                        className={`w-[70%] h-full select-none p-4 flex flex-col gap-2 ${
                          item.id == info.id ? "text-[#7a7a7a]" : ""
                        }`}
                      >
                        <h1 className="font-karla font-bold italic line-clamp-1">
                          {item.title}
                        </h1>
                        <p className="line-clamp-2 text-xs italic font-outfit font-extralight">
                          {item.description}
                        </p>
                      </div>
                    </Link>
                  );
                })
              : fallback.map((item) => {
                  return (
                    <Link
                      href={`/anime/watch/${item.id}/${data.id}`}
                      key={item.id}
                      className={`bg-secondary flex-center w-full h-[50px] rounded-lg scale-100 transition-all duration-300 ease-out ${
                        item.id == info.id
                          ? "pointer-events-none ring-1 ring-action text-[#5d5d5d]"
                          : "cursor-pointer hover:scale-[1.02] ring-0 hover:ring-1 hover:shadow-lg ring-white"
                      }`}
                    >
                      Episode {item.number}
                    </Link>
                  );
                })}
          </div>
        </div>
      </div>
    </>
  );
}

export async function getServerSideProps(context) {
  const { info } = context.query;
  if (!info) {
    return {
      notFound: true,
    };
  }

  const id = info[0];
  const aniId = info[1];
  const seek = info[2] || 0;
  let epiFallback = null;

  const res = await fetch(`https://api.moopa.my.id/meta/anilist/watch/${id}`);
  const epiData = await res.json();

  const res2 = await fetch(
    `https://api.moopa.my.id/meta/anilist/info/${aniId}`
  );
  const aniData = await res2.json();

  if (aniData.episodes.length === 0) {
    const res = await fetch(
      `https://api.moopa.my.id/anime/gogoanime/${
        aniData.title.romaji || aniData.title.english
      }`
    );
    const data = await res.json();
    const match = closestMatch(
      aniData.title.romaji,
      data.results.map((item) => item.title)
    );
    const anime = data.results.filter((item) => item.title === match);
    if (anime.length !== 0) {
      const infos = await fetch(
        `https://api.moopa.my.id/anime/gogoanime/info/${anime[0].id}`
      ).then((res) => res.json());
      epiFallback = infos.episodes;
    }
  }

  return {
    props: {
      info: {
        id,
        seek,
        epiData,
        aniData,
        epiFallback,
      },
    },
  };
}